<?php
$section = 'design';
$type = Application::TYPE_DESIGN;
$titre = _LB_DESIGN_MANAGEMENT;
$smarty->assign("titre", $titre);

//get object ID
$id = Request::getIntNumber('id');
$act = Request::getIntNumber('act');

//get back link
$backlink = Session::getBackLink($section,"list/?");

$arr_categories = array();
$related_category = new Related_Category();

$app = new Application($type);
if($id)
{
	if(!$app->get($id) || $app->TYPE != $type)
		redirect($backlink);

	$arr_categories = $related_category->arrayApplication($id);
}

$apps = Application::getTree(0, $type);
$cats = Category::getTree();

//if action = UPDATE or CREATE
if ($act == ACT_VALID_EDIT)
{
	$app->TITLE = trim(Request::get('TITLE'));
	$app->TYPE = $type;
	$app->DESCRIPTION = trim($_POST['DESCRIPTION']);
	$app->POS = Request::getNumber('POS');
	$app->ID_PARENT = Request::getNumber('ID_PARENT');
    $app->DISABLED = Request::getNumber('DISABLED');
	$arr_categories = Request::get('ID_CATEGORY');

	if ($app->validateInput())
	{
		if(!$app->ID){
			$app->insert();
        }
		else{
			$related_category->deleteApplication($app->ID);
			$app->update();
        }
		$related_category->insertApplication($app->ID, $arr_categories);

		redirect($backlink);
	}
}

include_once CKEDITOR_BASE_DIR. "ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = CKEDITOR_BASE_PATH;
$CKEditor->config['height'] = 600;
$CKEditor->config['filebrowserBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html';
$CKEditor->config['filebrowserImageBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Images';
$CKEditor->config['filebrowserFlashBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Flash';
$CKEditor->config['filebrowserUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Files';
$CKEditor->config['filebrowserImageUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Images';
$CKEditor->config['filebrowserFlashUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Flash';
//create cfer
$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_DESIGN_MANAGEMENT => $backlink,
	($id == 0? _LB_ADD_DESIGN : _LB_EDIT_DESIGN) => Utils::getCurrentLink() ));

//if validate is false, display form again
$smarty->assign('cfer', $cfer);
$smarty->assign('app', $app);
$smarty->assign('apps', $apps);
$smarty->assign('cats', $cats);
$smarty->assign('arr_categories', $arr_categories);
$smarty->assign('type', $type);
$smarty->assign('type_application', Application::TYPE_APPLICATION);
$smarty->assign('type_design', Application::TYPE_DESIGN);
$smarty->assign('type_technology', Application::TYPE_TECHNOLOGY);

$smarty->assign('backlink', $backlink);
$smarty->assign('CKEditor', $CKEditor);
$smarty->display('application_form.html');